/*
 * ============================================================================
 *                   GNU Lesser General Public License
 * ============================================================================
 *
 *
 *
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 * 
 *
 *
 */
package rad.framework.lang;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;

/**
 * This is a utility class that provide convenience methods for working with
 * Dates. Feel free to use these or not.
 * 
 * @author
 * @version 1.0
 * 
 */
public class DateUtil {

	/**
	 * format = MM/dd/yyyy
	 */
	public static final String BASIC_FORMAT = "MM/dd/yyyy";

	public static Date now() {
		return new Date();
	}

	public static Date parseBasicFormat(String date) throws ParseException {
		return parse(date, BASIC_FORMAT);
	}

	public static Date parse(String date, String format) throws ParseException {
		return DateUtils.parseDate(date, new String[] { format });
	}

	public static String format(Date date) {
		return format(date, BASIC_FORMAT);
	}

	public static String format(Date date, String format) {
		return DateFormatUtils.format(date, format);
	}

	public static Date truncateDate(Date d) {
		return DateUtils.truncate(d, Calendar.DATE);
	}

	public static Date endOfDay(Date d) {
		d = DateUtils.truncate(d, Calendar.DATE);
		d = DateUtil.addHours(d, 23);
		d = DateUtil.addMinutes(d, 59);
		d = DateUtil.addSeconds(d, 59);
		return d;
	}

	public static Date addSeconds(int seconds) {
		return DateUtils.addSeconds(new Date(), seconds);
	}

	public static Date addSeconds(Date date, int seconds) {
		return DateUtils.addSeconds(date, seconds);
	}

	public static Date addMinutes(int minutes) {
		return DateUtils.addMinutes(new Date(), minutes);
	}

	public static Date addMinutes(Date date, int minutes) {
		return DateUtils.addMinutes(date, minutes);
	}

	public static Date addDays(int days) {
		return DateUtils.addDays(new Date(), days);
	}

	public static Date addMonths(int months) {
		return DateUtils.addMonths(new Date(), months);
	}

	public static Date addDays(Date date, int days) {
		return DateUtils.addDays(date, days);
	}

	public static Date addMonths(Date date, int months) {
		return DateUtils.addMonths(date, months);
	}

	public static Date addYears(Date date, int years) {
		return DateUtils.addYears(date, years);
	}

	public static Date addHours(int hours) {
		return DateUtils.addHours(new Date(), hours);
	}

	public static Date addHours(Date date, int hours) {
		return DateUtils.addHours(date, hours);
	}

	public static Date setHour(int hour) {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.HOUR_OF_DAY, hour);
		return c.getTime();
	}

	public static boolean isDayOfWeek(int day) {
		Calendar c = Calendar.getInstance();
		int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
		return dayOfWeek == day;
	}

	/**
	 * Returns the number of days between <i>a</i> and <i>b</i>. If <i>b</i>
	 * is later than <i>a</i>, retuns a negative number i.e. <i>a</i> - <i>b</i>.
	 */
	public static int daysDifference(Date start, Date end) {
		Calendar startCal = Calendar.getInstance();
		Calendar endCal = Calendar.getInstance();

		startCal.setTime(start);
		endCal.setTime(end);

		return daysDifference(startCal, endCal);
	}

	public static boolean after(Date source, Date target) {
		Calendar sourceCal = Calendar.getInstance();
		Calendar targetCal = Calendar.getInstance();

		sourceCal.setTime(source);
		targetCal.setTime(target);

		return sourceCal.after(targetCal);
	}

	public static int daysDifference(Calendar start, Calendar end) {
		int multiplier = 1;
		if (start.after(end)) {
			java.util.Calendar swap = start;
			start = end;
			end = swap;
			multiplier = -1;
		}
		int days = end.get(java.util.Calendar.DAY_OF_YEAR)
				- start.get(java.util.Calendar.DAY_OF_YEAR);
		int y2 = end.get(java.util.Calendar.YEAR);
		if (start.get(java.util.Calendar.YEAR) != y2) {
			start = (java.util.Calendar) start.clone();
			do {
				days += start.getActualMaximum(java.util.Calendar.DAY_OF_YEAR);
				start.add(java.util.Calendar.YEAR, 1);
			} while (start.get(java.util.Calendar.YEAR) != y2);
		}
		return multiplier * days;
	}
}
